<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected $_frontController;
	protected $_config;
	protected $_request;
	
	public function __construct($application){
		parent::__construct($application);
		$this->_frontController = Zend_Controller_Front::getInstance();

	}

	protected function _initAutoload()
    {
	    $autoloader = Zend_Loader_Autoloader::getInstance();
		$autoloader->registerNamespace('Third_');
		
		$autoloader->suppressNotFoundWarnings(false);
		
		return $autoloader;
	}

	protected function _initSetupEnvironment(){
		error_reporting(E_ALL|E_STRICT);
		ini_set('display_errors', 'on');

        date_default_timezone_set('America/Fortaleza');
        setlocale(LC_ALL,'pt_BR');
        
        $locale = new Zend_Locale('pt_BR');
        Zend_Registry::set('Zend_Locale', $locale);
                
        $configType = (isset($_SERVER['SERVER_NAME']) && ($_SERVER['SERVER_NAME'] == '127.0.0.1' OR $_SERVER['SERVER_NAME'] == 'localhost')) ? 'development' : 'production';
        define('APPLICATION_ENVIRONMENT', $configType);
    }

   protected function _initSetupErrorHandler() {
        $this->_frontController->throwExceptions(true);

        $this->_frontController->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(
            array(
                'module'     => 'third',
                'controller' => 'error',
                'action'     => 'error')
            ));

        $writer = new Zend_Log_Writer_Firebug();
        $logger = new Zend_Log($writer);
        Zend_Registry::set('logger',$logger);
    }

	protected function _initConfigs(){
		$configs = $this->_options['configs'];
		$this->_configs = $configs;
		Zend_Registry::set ( 'configs', $configs );
	}

    protected function _initDbAdapter()
	{
		$this->bootstrap('db');
		$dbAdapter = $this->getResource('db');
		Zend_Registry::set('db', $dbAdapter);
	}

	protected function _initCache(){
		//Cache Options 
		$frontendOptions = array('lifetime' => 7200,'automatic_serialization' => true);
		$backendOptions = array('cache_dir' => APPLICATION_PATH.DIRECTORY_SEPARATOR.'cache');
		$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
		Zend_Registry::set('cache',$cache); 
	}

	protected function _initSite() { 
		try{
			require_once ("Sites.php"); 
			$site = new Sites(); 
	
			$domain = $_SERVER['HTTP_HOST'];
			
			$site = $site->getByDomain( $domain );
			Zend_Registry::set('site', $site );
		} catch( Zend_Excetion $e ){
			echo $e->getMessage();
		}
	}

	protected function _initModules(){
		if( !defined('MODULES_PATH'))
			define('MODULES_PATH', APPLICATION_PATH.'/modules' );

		$this->_frontController->setDefaultModule('default');
		$this->_frontController->setParam('prefixDefaultModule', false);
		
		$modulesPaths = array(
			'default' => APPLICATION_PATH.'/',
		    'third' => MODULES_PATH.'third/'
		);
		Zend_Registry::set('registered_modules', $modulesPaths );
		
		$modules = array(
			'default' => APPLICATION_PATH.'/controllers',
		    'third' => MODULES_PATH.'third/controllers'
		);

		$this->_frontController->setControllerDirectory( $modules );
	}

	protected function _initRequestOptions(){
		$router = new Zend_Controller_Router_Rewrite();
		$request =  new Zend_Controller_Request_Http();
		$this->_request = $router->route($request);
	}

	protected function _initView()
	{
		// Initialize view
        $view = new Zend_View();

        // Add it to the ViewRenderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
            'ViewRenderer'
        );

        $view->layout_base = $this->_configs['third']['layout_base'];

        $viewRenderer->setView($view);

        // Return it, so that it can be stored by the bootstrap
        return $view;

    }

	protected function _initViewHelpers(){
		$this->bootstrap('layout');

		$layout = $this->getResource('layout');

		$view = $layout->getView();

		$view->doctype('XHTML1_STRICT');
		$view->setEncoding('UTF-8'); 

				// META
 		$view->headMeta()
 					->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8') 
                  	->appendHttpEquiv('Content-Language', 'pt-BR');
                  	
		$view->headTitle("Zend Form Builder", 'APPEND');

		$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
		$viewRenderer->setView( $view );
		Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

		$view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');

		$base = $view->layout_base;
		
		$view->jQuery()->setLocalPath( $base . '/default/js/jquery-1.3.2.min.js')
		->setUiLocalPath( $base . '/default/js/jquery-ui-1.7.2.custom.min.js')
		->addJavascriptFile( $base . '/default/js/jquery-ui-1.7.2.custom.min.js')
		->addJavascriptFile( $base . '/default/js/jquery.form.js')
		->addJavascriptFile( $base . '/default/js/jquery.validade.js')
		->enable()
		->uiDisable();
		
		//PAGINATOR PARTIAL
		Zend_View_Helper_PaginationControl::setDefaultViewPartial(
			'paginator.phtml'
		);

		Zend_Registry::set('view', $view );
	}

	protected function _initAcl()
    {
    	$auth = new Third_Auth();
    	Zend_Registry::set ( 'auth' ,  $auth);

    	$module = $this->_request->getModuleName();
    	
    	if( $module != 'default' ){
	    	$auth = Zend_Auth::getInstance();
	    	
	        $acl = new Third_Acl( $auth );
	
	    	
	        $this->_frontController->setParam('auth', $auth );
	        $this->_frontController->setParam('acl', $auth );
	
	        $this->_frontController->registerPlugin(
	            new Third_Plugins_Acl( $auth, $acl)
	        );
    	}
    }
}

